<!--  -->
<template>
  <div id="main">
    <h2>广告管理</h2>

    <el-button type="primary" style="margin:20px" @click="()=>{handleAdd()}">添加广告</el-button>

    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <!-- <el-table-column
          align="center"
          prop="adId"
          label="广告编号"
        /> -->

        <el-table-column
          label="序号"
          type="index"
          width="80"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="adName"
          label="广告名称"
        />

        <el-table-column
          align="center"
          prop="adType"
          label="广告类型"
        >
          <template slot-scope="Scope">
            <span v-if="Scope.row.adType==1">服务项目广告</span>
            <span v-if="Scope.row.adType==2">第三方广告</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="adImg"
          label="广告图片"
          width="250px"
        >
          <template slot-scope="Scope">
            <img :src="Scope.row.adImg" width="170px" height="75px">
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="adState"
          label="当前状态"
        >
          <template slot-scope="Scope">
            <span v-if="Scope.row.adState==0" style="color:red">未展示</span>
            <span v-if="Scope.row.adState==1" style="color:green">已展示</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" width="400px">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleEdit(Scope.row.adId)">修改信息</el-button>
            <el-button type="primary" icon="el-icon-edit" @click="statusVisible=true;id=Scope.row.adId">修改状态</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="handleDelete(Scope.row.adId,Scope.row.adName)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <adDetail :id="id" :visible="editVisible" :type="type" :title="title" @close="handleClose" />

    <el-dialog title="修改广告状态" :visible.sync="statusVisible">
      <el-form :model="status">
        <el-form-item label="状态：">
          <el-select v-model="status" placeholder="请选择">
            <el-option key="0" label="未展示" value="0" />
            <el-option key="1" label="已展示" value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleUpdateStatus">修 改</el-button>
        <el-button @click="statusVisible=false">返 回</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getAllAd, getAdById, deleteById, updateAd } from '@/api/ad'
import adDetail from '@/views/ad/adDetail'
export default {
  name: 'Ad',
  // import引入的组件需要注入到对象中才能使用
  components: {
    'adDetail': adDetail
  },
  data() {
    // 这里存放数据
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      listLoading: false,
      dialogFormVisible: false,
      pwdFormVisible: false,
      consoleUser: {},
      changePwd: {},
      id: 0,
      editVisible: false,
      statusVisible: false,
      type: 0,
      title: '',
      status: '',
      ad: ''
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.listLoading = true
      getAllAd().then((data) => {
        this.list = data.obj
        this.listLoading = false
      })
    },
    handleAdd() {
      this.title = '添加广告'
      this.type = 1
      this.editVisible = true
    },
    handleClose() {
      this.editVisible = false
    },
    handleEdit(id) {
      this.id = id
      this.title = '编辑广告'
      this.type = 1
      this.editVisible = true
      this.type = 2
    },
    handleUpdateStatus() {
      getAdById(this.id).then((resp) => {
        this.ad = resp.obj
        this.ad.adState = this.status
        console.log(this.ad)
        this.statusVisible = false
        this.fetchData()
        updateAd(this.ad).then(() => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          this.fetchData()
        })
      })
    },
    handleDelete(id, name) {
      this.$confirm('确认删除该广告？广告名称为:' + name).then(() => deleteById(id).then((data) => {
        this.$notify({
          title: '操作成功',
          type: 'success'
        })
        const totalPage = Math.ceil((this.list.length - 1) / this.pagesize)
        const page = this.currentPage > totalPage ? totalPage : this.currentPage
        this.currentPage = this.currentPage < 1 ? 1 : page
        this.fetchData()
      }))
    }

  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style  scoped>
    #main{
        margin-left: 5px;
        margin-right: 5px;
    }

    #table-area{

        margin-left: 10px;
        margin-right: 10px;
    }

</style>
